package com.action.app_qrcode;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Category;
import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.QRcodeBuilder.CreateQr;
import com.QRcodeBuilder.NetQrEntity;
import com.QRcodeBuilder.NetQrVo;
import com.dataProcess.VerifyResou;
import com.enetiy.Areainfo;
import com.enetiy.Areaip;
import com.enetiy.Binding;
import com.enetiy.Comboinfo;
import com.enetiy.Userinfo;
import com.operation.NET;
import com.service.AreaIpService;
import com.service.AreaService;
import com.service.AreainfoService;
import com.service.AreausbleService;
import com.service.BindingService;
import com.service.ComboService;
import com.service.MapService;
import com.service.NetLinkService;
import com.service.UserinfoService;
import com.util.DataUtil;
import com.util.Log4gUtil;
import com.util.NetTimeUtil;
import com.util.dataFormatUtil;

public class accreditQrAction {
	private Category log =Log4gUtil.getLog();
	private String params;
	private String sessionid;
	private int siteAreaid;
	private int userid;
	private String ip;
	private String mac;
	private String phone;
	private String nickname;
	private int band;
	private String headimg;
	private String NF_callid;
	private String token;
	private String apMac;
	
	private BindingService bindingService;
	private AreausbleService areausbleService;
	private AreaIpService areaIpService;
	private ComboService comboService;
	private AreainfoService areainfoService;
	private AreaService areaService;
	private NetLinkService netLinkService;
	private MapService mapService;

	
	
	/**
	 * 
	 *app验证授权二维码   >Nf042
	 */
	public  void verifyNetQr(){
		 log.info("verifyNetQr-进入app二维码授权-userid:"+userid+"-数据:"+params);
		if(params!=null&&!params.equals("")){
			 JSONObject dataParser = DataUtil.dataParser(params);
			 if(dataParser.has("sessionid"))
				sessionid =  dataParser.getString("sessionid");
			 if(dataParser.has("band"))
				 band =  dataParser.getInt("band");
			 if(dataParser.has("apMac"))
				 apMac =  dataParser.getString("apMac");
		 }
		log.info("verifyNetQr-进入app二维码授权-userid:"+userid+"-数据:"+params);
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out =null;
		net.sf.json.JSONObject sendJson = DataUtil.sendJson();
		try {
			out = response.getWriter();
			if(band!=0){
				NetQrVo netQrVo = NetQrVo.getNetQrVo();
	    		NetQrEntity netQr= netQrVo.getNetQrEntityMap().get(sessionid);
				if(netQr!=null){
					NET nets = new NET();
					Areainfo areainfo = areainfoService.selectAreainfo(netQr.getSiteAreaid(), "NF-net-40");
					Areaip areaip = areaIpService.selectAreaip(netQr.getSiteAreaid());
					int netAllow = nets.netAllow("", "", netQr.getSiteAreaid(),netQr.getIp(), band, areainfo.getArea().getNetOnline(),
							NetTimeUtil.getTimeUtil(new Date(), areainfo.getValidTime()), areaip.getPublicIp(), netQr.getUserid(),
							netQr.getMac(),netQr.getPhone(),2,1,NF_callid,token,apMac,"",mapService,areausbleService,netLinkService);
					if(netAllow==1){
						netQr.setBand(band);
						netQrVo.getNetQrEntityMap().put(sessionid, netQr);
						sendJson.put("code", 1);
						sendJson.put("msg","联网成功");
					}else{//联网失败
						sendJson.put("code", -1);
						sendJson.put("msg","联网失败");
					}
				}else{
					sendJson.put("code", 0);
					sendJson.put("reason", 1);
					sendJson.put("msg","缓存sessionid不存在");
				}
			}else{
				sendJson.put("code", 0);
				sendJson.put("reason", 2);
				sendJson.put("msg","无资源");
			}
		} catch (Exception e) {
			sendJson.put("code", -1);
			sendJson.put("msg","服务器异常");
		}
		out.print(sendJson);
		log.info("verifyNetQr-进入app二维码授权-userid:"+userid+"-数据:"+sendJson.toString());
	}
	
	
	/**
	 * 
	 * 授权二维码提示内容  >Nf043
	 */
	public  void infoNetQr(){
		 log.info("infoNetQr-进入app二维码提示内容-userid:"+userid+"-数据:"+params);
		if(params!=null&&!params.equals("")){
			 JSONObject dataParser = DataUtil.dataParser(params);
			 if(dataParser.has("sessionid"))
				sessionid =  dataParser.getString("sessionid");
		 }
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out =null;
		
		net.sf.json.JSONObject sendJson = DataUtil.sendJson();
		
		VerifyResou verifre = new VerifyResou();
		try {
			out = response.getWriter();
			int band =0;
			Binding binding = null;
			Comboinfo comboinfo = null;
			Userinfo userinfo = null;
			NetQrVo netQrVo = NetQrVo.getNetQrVo();
			NetQrEntity netQr= netQrVo.getNetQrEntityMap().get(sessionid);
			if(netQr!=null){
				String netTime="";
				Areainfo areainfo = areaService.selsctAreainfo(netQr.getSiteAreaid(), -1);
				//绑定资源
				if((binding=verifre.verifyBinding(userid,netQr.getSiteAreaid(), areausbleService, bindingService))!=null){
					band =binding.getAreainfo().getAreatype().getBanfwidth();
					if(binding.getArea().getId()==1&&binding.getTypeExplain()!=null
							&&binding.getTypeExplain().equals("老师")){
						netTime = dataFormatUtil.getHour(1440);
					}else if(binding.getArea().getId()==1&&binding.getTypeExplain()!=null
							&&binding.getTypeExplain().equals("学生")){
						netTime = dataFormatUtil.getHour(120);
					}else{
						netTime = dataFormatUtil.getHour(120);
					}
				}else{//套餐资源
					/*if((comboinfo=verifre.verifyCombo(userid, netQr.getSiteAreaid(), areainfoService ,comboService))!=null){
					//套餐待定
					band =0;
					sendJson.put("netTime",dataFormatUtil.getHour(areainfo.getValidTime()));*/
				}
				if(band!=0){
						sendJson.put("code", 1);
						sendJson.put("netTime",netTime);
						sendJson.put("phone", netQr.getPhone());
						sendJson.put("siteAreaid", netQr.getSiteAreaid());
						sendJson.put("nickname", netQr.getNickname());
						sendJson.put("headimg", netQr.getHeadimg());
						sendJson.put("band",band);
					}else{
						sendJson.put("code", 0);
						sendJson.put("reason", 2);
						sendJson.put("msg","无资源");
					}
			}else{
				sendJson.put("code", 0);
				sendJson.put("reason", 1);
				sendJson.put("msg","缓存sessionid不存在");
			}
		} catch (Exception e) {
			sendJson.put("code", -1);
			sendJson.put("msg","服务器异常");
		}
		out.print(sendJson);
		 log.info("infoNetQr-输出app二维码提示内容-userid:"+userid+"-数据:"+sendJson.toString());
	}
	
	/**
	 *app授权上网-二维码内容  >Nf040
	 */
	public void createNetQr(){
		log.info("createNetQr-进入app二维码生成-userid:"+userid+"-数据:"+params);
		if(params!=null&&!params.equals("")){
			 JSONObject dataParser = DataUtil.dataParser(params);
			 if(dataParser.has("userid"))
				userid =  dataParser.getInt("userid");
			 if(dataParser.has("ip"))
				ip=  dataParser.getString("ip");
			 if(dataParser.has("mac"))
				mac=  dataParser.getString("mac");
			 if(dataParser.has("phone"))
				phone=  dataParser.getString("phone");
			 if(dataParser.has("nickname"))
				 nickname=  dataParser.getString("nickname");
			 if(dataParser.has("headimg"))
				 headimg=  dataParser.getString("headimg");
		 }
		log.info("createNetQr-进入app二维码生成-userid:"+userid+"-params"+params);
			HttpServletResponse response = ServletActionContext.getResponse();
			PrintWriter out =null;
			net.sf.json.JSONObject sendJson = DataUtil.sendJson();
			try {
				out = response.getWriter();
				CreateQr createQr = new CreateQr();
				boolean qr = createQr.createNetQr(userid, ip, mac, nickname, phone, siteAreaid,headimg);
				if(qr){
					sendJson.put("code", 1);
					sendJson.put("text",createQr.text);
					sendJson.put("sessionid", createQr.sessionid);
					
				}else{
					sendJson.put("code", 0);
					sendJson.put("msg", "数据有误");
				}
		} catch (Exception e) {
			sendJson.put("code", -1);
			sendJson.put("msg", "服务器异常");
			e.printStackTrace();
		}
		out.print(sendJson);
		log.info("createNetQr-输出app二维码生成-userid:"+userid+"-数据:"+sendJson.toString());
	}
	
	/**
	 * 二维码扫描检查登录  >Nf041
	 * @return
	 */
	public void  checkNetQr(){
		log.info("checkNetQr-进入app二维码检查登录-userid:"+userid+"-数据:"+params);
		if(params!=null&&!params.equals("")){
			 JSONObject dataParser = DataUtil.dataParser(params);
			 if(dataParser.has("sessionid"))
				sessionid =  dataParser.getString("sessionid");
		 }
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out =null;
		net.sf.json.JSONObject sendJson = DataUtil.sendJson();
		
		long inTime = new Date().getTime();
        Boolean bool = true;
        boolean flg = false;
        NetQrVo netQrVo =null;
        while (bool) {
            try {
            	out = response.getWriter();
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //检测登录
        	netQrVo = NetQrVo.getNetQrVo();
    		NetQrEntity netQrEntity= netQrVo.getNetQrEntityMap().get(sessionid);
    		if(netQrEntity!=null&&netQrEntity.getBand()!=0){
    			 bool = false;
    			 flg = true;
            }else{
                if(new Date().getTime() - inTime > 18000){
                    bool = false;
                }
            }
        }
        if(flg)
        	sendJson.put("code", 1);
        else
        	sendJson.put("code", 0);
        
        //删除缓存超时数据
        netQrVo.Mapdelete(netQrVo.getNetQrEntityMap());
        out.print(sendJson);
        log.info("checkNetQr-输出app二维码检查登录-userid:"+userid+"-数据:"+sendJson.toString());
	}
	
	
	public void setParams(String params) {
		this.params = params;
	}

	public void setSessionid(String sessionid) {
		this.sessionid = sessionid;
	}

	public void setSiteAreaid(int siteAreaid) {
		this.siteAreaid = siteAreaid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public void setMac(String mac) {
		this.mac = mac;
	}

	public void setBindingService(BindingService bindingService) {
		this.bindingService = bindingService;
	}

	public void setAreausbleService(AreausbleService areausbleService) {
		this.areausbleService = areausbleService;
	}

	public void setAreaIpService(AreaIpService areaIpService) {
		this.areaIpService = areaIpService;
	}

	public void setComboService(ComboService comboService) {
		this.comboService = comboService;
	}

	public void setAreainfoService(AreainfoService areainfoService) {
		this.areainfoService = areainfoService;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public void setBand(int band) {
		this.band = band;
	}



	public void setHeadimg(String headimg) {
		this.headimg = headimg;
	}


	public void setLog(Category log) {
		this.log = log;
	}


	public void setNF_callid(String nF_callid) {
		NF_callid = nF_callid;
	}


	public void setToken(String token) {
		this.token = token;
	}


	public void setMapService(MapService mapService) {
		this.mapService = mapService;
	}


	public void setApMac(String apMac) {
		this.apMac = apMac;
	}


	public void setNetLinkService(NetLinkService netLinkService) {
		this.netLinkService = netLinkService;
	}

}
